package vistas;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.Vector;

import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

import entidades.Mensajes;


import rmi.InterfazRemota;
import rmi.ObjetoRemoto;

public class MDIChildEnviados extends JInternalFrame {
	private String[] columnNames = {"Nro.Mensaje", "De", "Para", "asunto", "Cuerpo", "Fecha", "Estado", "Prioridad"};
	private Object[][] data;
	private JTable tblCorreos;
	private InterfazRemota objetoRemoto;
	private int id_casilla;
	private int id_mensaje;
	private Vector<Mensajes> vectorTemporal;
	private JButton btnAbrir;
	private JButton btnEliminar;
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final int SEPARACION = 24;
	static int inset = 0;
	private MDI mdi;

    public MDIChildEnviados(MDI mdi, int id_casilla) {
        super(null,true,true,true,true);
    	this.id_casilla=id_casilla;
        this.mdi = mdi;
        objetoRemoto = mdi.getObjetoRemoto();
        inicializar();
        setLocation(SEPARACION*inset, SEPARACION*inset);
        ++inset;
    }
    
    private void inicializar() {
    	vectorTemporal = new Vector<Mensajes>(); 
    	try {
			vectorTemporal = objetoRemoto.listarCorreosEnviados(id_casilla);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		data = new Object[vectorTemporal.size()][8];
		for (int i = 0; i<vectorTemporal.size();i++){
			for (i=0;i<vectorTemporal.size();i++){
				data [i][0]=  vectorTemporal.elementAt(i).getId_mensaje();
				data [i][1]= vectorTemporal.elementAt(i).getId_casilla_origen();
				data [i][2]= vectorTemporal.elementAt(i).getId_casilla_destino();
				data [i][3]= vectorTemporal.elementAt(i).getAsunto();
				data [i][4]= vectorTemporal.elementAt(i).getTexto();
				data [i][5]= vectorTemporal.elementAt(i).getFecha();
				data [i][6]= vectorTemporal.elementAt(i).getEstado();
				data [i][7]= vectorTemporal.elementAt(i).getPrioridad();
				//System.out.println("cargo el data"+i);
								
			}
		}
	
        setName("mdiChild");
        btnAbrir =  new JButton("Abrir");
        btnAbrir.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Vector<String> datos = new Vector<String>();
				for (int i=0;i<8;i++){
					Object objeto = tblCorreos.getValueAt(tblCorreos.getSelectedRow(), i);
					datos.add(objeto.toString());
				}

				mdi.setDatos(datos);
				mdi.abrirCorreo(e);	
				//System.out.println(objeto.toString());
				
			}
        	
        });
        btnEliminar = new JButton("Eliminar");
        btnEliminar.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				//Object objeto =	tblCorreos.getValueAt(tblCorreos.getSelectedRow(), tblCorreos.getSelectedColumn());
				Object objeto =	tblCorreos.getValueAt(tblCorreos.getSelectedRow(), 0);
				id_mensaje = Integer.parseInt(objeto.toString());
				
				try {
					System.out.println(objetoRemoto.eliminarCorreoEnviado(id_casilla, id_mensaje));
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
        	
        });
        
        tblCorreos =  new JTable(data,columnNames);
        JScrollPane scrollPane =  new JScrollPane(tblCorreos);
        tblCorreos.setFillsViewportHeight(true);
                
        Container container = getContentPane();
        container.setLayout(new BorderLayout());
        container.add(tblCorreos.getTableHeader(),BorderLayout.PAGE_START);
        container.add(tblCorreos,BorderLayout.CENTER);
        
        //panel inferior
        Panel panel =  new Panel();
        panel.setLayout(new GridLayout(1,2));        
        panel.add(btnAbrir);
        panel.add(btnEliminar);
        
        container.add(panel, BorderLayout.SOUTH);
        
        int chWidth = (int) mdi.getWidth()*55/100;
        int chHeight = (int) mdi.getHeight()*50/100;
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-chWidth)/2, (screenSize.height-chHeight)/2, chWidth, chHeight);
     }





}
